package ua.com.infinite.mobile.adapters;

import org.json.JSONObject;

import ua.com.infinite.mobile.App;
import ua.com.infinite.mobile.R;
import ua.com.infinite.mobile.contentprovider.InfiniteContentProvider;
import ua.com.infinite.mobile.db.DBAdapter;
import ua.com.infinite.mobile.db.TableTransaktions;
import ua.com.infinite.mobile.server.Constants;
import ua.com.infinite.mobile.server.DataLoaderTransactions;
import ua.com.infinite.mobile.server.OnDownloadingListener;
import ua.com.infinite.mobile.server.OnUpdateUiListener;
import ua.com.infinite.mobile.server.RequestObject;
import ua.com.infinite.mobile.utils.PhoneUtils;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class AdapterTransaktions extends CursorAdapter implements OnDownloadingListener{
	
	private String firstDate, lastDate;
	private String tag;
	private LayoutInflater layoutInflater;
	private Context context;
	private DBAdapter db;
	private Cursor cursor;
	private DataLoaderTransactions dataLoaderTransactions;
	private OnUpdateUiListener onUpdateUiListener;

	public AdapterTransaktions(Context context, Cursor c, OnUpdateUiListener onUpdateUiListener, String tag) {
		super(context, c);
		this.tag = tag;
		this.layoutInflater = LayoutInflater.from(context);
		this.context = context;
		this.db = new DBAdapter(context);
		this.onUpdateUiListener = onUpdateUiListener;
		if (cursor == null)
			update();
	}

	public void update() {
		int user_id = App.getUser().getId();
		String selection = TableTransaktions.COLUMN_USER_ID+"=?";
		String[] selectionArgs = new String[]{String.valueOf(user_id)};
		this.cursor = context.getContentResolver().query(InfiniteContentProvider.CONTENT_URI_TRANSAKTIONS, null, selection, selectionArgs, TableTransaktions.COLUMN_DATE+" DESC");
		if (cursor == null || !cursor.moveToFirst()){
			RequestObject requestObject = new RequestObject();
			requestObject.setAction(Constants.ACTIONS);
			requestObject.addParam(Constants.USER_ID, String.valueOf(user_id));
			startDownload(requestObject, false);
		}else{
			changeCursor(cursor);
		}
	}

	@Override
	public void startDownload(RequestObject request, boolean returnDataToCaller) {
		dataLoaderTransactions = new DataLoaderTransactions(context, this, returnDataToCaller);
		dataLoaderTransactions.execute(new RequestObject[]{request});
	}

	@Override
	public void onStartDownloading() {
		
	}

	@Override
	public void succesDownloading(boolean isSucces) {
		if (isSucces) {
			update();
		}
		onUpdateUiListener.updateUI(isSucces);
		
	}

	@Override
	public void returnData(JSONObject object) {
		
	}

	@Override
	public View newView(Context paramContext, Cursor paramCursor, ViewGroup paramViewGroup) {
		View row = layoutInflater.inflate(R.layout.transaktions_row, null, true);
		TransaktionsViewHolder holder = new TransaktionsViewHolder();
		holder.name = (TextView) row.findViewById(R.id.name);
		holder.cardNumber = (TextView) row.findViewById(R.id.card_number);
		holder.payment = (TextView) row.findViewById(R.id.payment);
		holder.bonus = (TextView) row.findViewById(R.id.bonus);
		holder.bonus_ballance = (TextView) row.findViewById(R.id.bonus_ballance);
		holder.comment = (TextView) row.findViewById(R.id.comment);
		holder.date = (TextView)row.findViewById(R.id.date);
		row.setTag(holder);
		return row;
	}

	@Override
	public void bindView(View paramView, Context paramContext, Cursor cursor) {
		TransaktionsViewHolder holder = (TransaktionsViewHolder) paramView.getTag();
		int id = cursor.getInt(cursor.getColumnIndex(TableTransaktions.COLUMN_ID));
		int partner_id = cursor.getInt(cursor.getColumnIndex(TableTransaktions.COLUMN_PARTNER_ID));
		String partner_name = cursor.getString(cursor.getColumnIndex(TableTransaktions.COLUMN_PARTNER_NAME));
		String card_number = cursor.getString(cursor.getColumnIndex(TableTransaktions.COLUMN_CARD_NUMBER));
		int action_type= cursor.getInt(cursor.getColumnIndex(TableTransaktions.COLUMN_ACTION_TIPE));
		int payment= cursor.getInt(cursor.getColumnIndex(TableTransaktions.COLUMN_PAYMENT));
		int bonus= cursor.getInt(cursor.getColumnIndex(TableTransaktions.COLUMN_BONUS));
		int bonus_balans= cursor.getInt(cursor.getColumnIndex(TableTransaktions.COLUMN_BALLANCE_BONUS));		
		String date = cursor.getString(cursor.getColumnIndex(TableTransaktions.COLUMN_DATE));
		String comment = cursor.getString(cursor.getColumnIndex(TableTransaktions.COLUMN_COMMENT));

		holder.name.setText(partner_name);
		holder.cardNumber.setText(card_number);
		holder.payment.setText(""+payment);
		holder.bonus.setText(""+bonus);
		holder.bonus_ballance.setText(""+bonus_balans);
		holder.date.setText(date);
		holder.comment.setText(comment);
		
		paramView.setBackgroundColor(PhoneUtils.getColorByStatus(context, action_type));
		
		
	}
	
	static class TransaktionsViewHolder{
		TextView name;
		TextView cardNumber;
		TextView payment;
		TextView bonus;
		TextView bonus_ballance;
		TextView date;
		TextView comment;
		
	}

}
